package com.zerui.manager.web;

import com.alibaba.fastjson.JSONObject;
import com.dahuatech.icc.exception.ClientException;
import com.zerui.cola.dto.Response;
import com.zerui.cola.dto.SingleResponse;
import com.zerui.manager.app.manager.AsyncManager;
import com.zerui.manager.app.manager.factory.AsyncFactory;
import com.zerui.manager.icc.service.AccessControlInfoService;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

@Validated
@Api(value = "通行记录", tags = "门禁设备管理")
@RestController
@RequestMapping("/api/v1/record")
public class RecordController {

    @Resource
    private AccessControlInfoService accessControlInfoService;

    /**
     * 该方法用于ICC订阅门禁报警事件回调
     *
     * @param jo 接收报警数据
     * @return
     */
    @ResponseBody
    @PostMapping("/iccEventReceiveMsg")
    public Response iccEventReceiveMsg(@RequestBody JSONObject jo) {
        System.out.println(jo.toJSONString());
        // 异步保存到数据表
        AsyncManager.me().execute(AsyncFactory.saveAccessData(jo));
        return Response.buildSuccess();
    }

    @ResponseBody
    @PostMapping("/iccSubscribeList")
    public Response iccSubscribeList() throws ClientException {
        JSONObject jsonObject = accessControlInfoService.listSubscribe();
        return SingleResponse.of(jsonObject);
    }

    @ResponseBody
    @PostMapping("/cancelSubscribe")
    public Response cancelSubscribe(@NotBlank(message = "请提供订阅名称") String magic) throws ClientException {
        JSONObject jsonObject = accessControlInfoService.listSubscribe();
        if (jsonObject.containsKey(magic))
            accessControlInfoService.cancelSubscribe(magic);
        return Response.buildSuccess();
    }

}
